﻿namespace IBTrader.Charts
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;

    class ImageGraphics : BaseGraphics, IGraphics
    {
        //private static float Width = 1366 * Factor, Height = 763 * Factor;
        //private static float Width = 1385 * Factor, Height = 670 * Factor;
        //protected static double Width = 1366 * Factor, Height = 768 * Factor;
        private readonly string fileName;
        private Graphics g;

        protected ImageGraphics() { }

        public ImageGraphics(string fileName)
        {
            this.fileName = fileName;
        }

        public void Draw(IEnumerable<IChart> charts,double width, double height)
        {
            using (Bitmap bmp = new Bitmap((int)width, (int)height))
            {
                using (g = Graphics.FromImage(bmp))
                {
                    Init(bmp);
                    Draw(this, charts, width, height);
                    Save(bmp);
                }
            }
        }

        protected virtual void Save(Bitmap bmp)
        {
            bmp.Save(fileName, ImageFormat.Png);
        }

        private void Init(Bitmap bmp)
        {
            g.DrawRectangle(Pens.Black, new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1));
            g.FillRectangle(new SolidBrush(Color.White), new RectangleF(1, 1, bmp.Width - 2, bmp.Height - 2));
        }

        #region IGraphics
        void IGraphics.FillPolygon(SolidBrush brush, PointF[] pointF)
        {
            g.FillPolygon(brush, pointF);
        }
        void IGraphics.DrawLines(Pen pen, params PointF[] points)
        {
            if (points != null && points.Length>1) g.DrawLines(pen, points);
        }
        void IGraphics.DrawString(string p, Font Font, SolidBrush solidBrush, PointF point)
        {
            g.DrawString(p, Font, solidBrush, point);
        }
        void IGraphics.FillRectangle(SolidBrush brush, RectangleF rectangle)
        {
            g.FillRectangle(brush, rectangle);
        }
        void IGraphics.DrawRectangle(Pen pen, Rectangle rectangle)
        {
            g.DrawRectangle(pen, rectangle);
        }
        void IGraphics.FillPie(SolidBrush brush, Rectangle rectangle, int startAngle, int sweepAngle)
        {
            g.FillPie(brush, rectangle, startAngle, sweepAngle);
        }
        void IGraphics.DrawPie(Pen pen, RectangleF rectangle, int startAngle, int sweepAngle)
        {
            g.DrawPie(pen, rectangle, startAngle, sweepAngle);
        }
        #endregion
    }
}
